class SongCollectionsController < ApplicationController
  #If AJAX request, use the slim layout (no header or footer)
  #Otherwise use the main layout
  layout Proc.new { |controller| controller.request.xhr? ? 'slim' : 'main' }

  def new
    @song = Song.find(params[:song_id])
  end
  
  def search
    @songs = SongCollection.search(params[:search])
	
	respond_to do |format|
	  format.html { }
	  format.js {
	    render :update do |page|
		  page.replace_html("song-search-results", :partial => 'results',
		                                           :locals => { :songs => @songs })
		end
	  }
	end
  end
  
  def create
    @song = Song.find(params[:song_id])
	
	if params[:collection_id]
	  @song.song_collection = SongCollection.find(params[:collection_id])
	else
	  new_collection = SongCollection.new(:name => params[:collection_name])
	  @song.song_collection = new_collection
	end
	
	respond_to do |format|
	  if @song.save
	    flash[:notice] = 'Success.'
		format.html { redirect_to_request_uri }
	  else
	    flash[:error] = @song.errors.full_messages
		format.html { redirect_to_request_uri }
	  end
	end
  end

end
